/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
* Name: directx_video_sink.cpp
*
* Purpose: video sink component implementation with directx video.
*
* Developer:
*   wen.gu , 2018-09-07
*
* TODO:
*
***************************************************************************/

/******************************************************************************
**    INCLUDES
******************************************************************************/
#define _CRT_SECURE_NO_WARNINGS

#include "mp_plugin.h"

#include "directx_video_sink.h"
#include "directx_audio_sink.h"


/******************************************************************************
**    MACROS
******************************************************************************/

#define DX_COMP_NAME "directx video"
#define DX_COMP_VER "00.01.00"


/******************************************************************************
**    VARIABLE DEFINITIONS
******************************************************************************/

/******************************************************************************
**   FUNCTION DEFINITIONS
******************************************************************************/
class Win32RenderPlugin : public MPPlugin
{
public:
    Win32RenderPlugin(const std::string& name,
        const std::string& version,
        const std::string& author
    );
    virtual ~Win32RenderPlugin();

public:
    virtual MPError createComponent(const MPCCreateParam* param, MPComponent** comp) override;
};

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
Win32RenderPlugin::Win32RenderPlugin(const std::string& name,
                                     const std::string& version,
                                     const std::string& author)
    :MPPlugin(name, version, author)
{
    /** todo something */
}

Win32RenderPlugin::~Win32RenderPlugin()
{
    /** todo something */
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

MPError Win32RenderPlugin::createComponent(const MPCCreateParam* param, MPComponent** comp)
{
    MPError ret = MP_ErrorBadParameter;

    if (param && comp)
    {
        if (param->type == MP_AUDIO_SINK_COMP_TYPE)
        {
            const MPAudioSinkCreateParam* ap = static_cast<const MPAudioSinkCreateParam*>(param);

            if (ap)
            {
                DxAudioSink* das = new DxAudioSink;

                ret = das->initialize(ap->info);

                if (MP_OK == ret)
                {
                    *comp = das;
                }
                else
                {
                    delete das;
                }
            }
        }
        else if (param->type == MP_VIDEO_SINK_COMP_TYPE)
        {
            const MPVideoSinkCreateParam* vp = static_cast<const MPVideoSinkCreateParam*>(param);

            if (vp)
            {
                const MPTrackInfo& ti = vp->info;

                if ((ti.category == eMediaCategory::Video) && (ti.format == eMediaFormat::YUV420Planar))
                {
                    DxVideoSink* dvs = new DxVideoSink;

                    ret = dvs->initialize(vp->info);

                    if (ret == MP_OK)
                    {
                        *comp = dvs;
                    }
                    else
                    {
                        delete dvs;
                    }
                }
                else
                {
                    ret = MP_ErrorUnsupportedType;
                }
            }
        }
        else
        {
            ret = MP_ErrorUnsupportedType;
        }
    }

    return ret;
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

/** the entry of current plugin */
MP_PLUGIN_ENTRY(Win32RenderPlugin,
    "win32 render plugin",  /** name */
    "00.01.00",           /** version */
    "454727014@qq.com" /** author */
);

